/**
 * jims
 */
package com.jims.master.modules.template.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.sys.entity.PerformFreqDict;
import com.jims.master.modules.sys.entity.SysOrgDict;
import com.jims.master.modules.sys.utils.PerformFreqUtils;
import com.jims.master.modules.sys.utils.SysOrgDictUtils;
import com.jims.master.modules.template.entity.TreatRecordItem;
import com.jims.master.modules.template.entity.DoctorTempla;
import com.jims.master.modules.template.entity.TreatProjectItem;
import com.jims.master.modules.template.service.DoctorTemplaService;
import com.jims.master.modules.template.service.TreatProjectItemService;
import com.jims.master.modules.template.service.TreatRecordItemService;
import com.jims.master.modules.utils.TemplateTreeTable;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;


/**
 * Controller
 * @author 系统管理员
 * @version 2017-06-02
 */
@Controller
@RequestMapping(value = "${adminPath}/treatProjectItem/treatProjectItem")
public class TreatProjectItemController extends BaseController {

	@Autowired
	private TreatProjectItemService treatProjectItemService;
	@Autowired
	private DoctorTemplaService doctorTemplaService;
	@Autowired
	private TreatRecordItemService treatRecordItemService;
	
	@ModelAttribute
	public TreatProjectItem get(@RequestParam(required=false) String id) {
		TreatProjectItem entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = treatProjectItemService.get(id);
		}
		if (entity == null){
			entity = new TreatProjectItem();
		}
		return entity;
	}
	
	@RequiresPermissions("user")
	@RequestMapping(value = {"indexTreat", ""})
	public String list(TreatProjectItem treatProjectItem,TreatRecordItem treatRecordItem,String id,String type,String kc, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<TreatProjectItem> treeList = new ArrayList<TreatProjectItem>();
		treatProjectItem.setApplyId(id);

		List<TreatProjectItem> list = treatProjectItemService.findList(treatProjectItem);
//		Page<TreatProjectItem> page = treatProjectItemService.findPage(new Page<TreatProjectItem>(request, response), treatProjectItem);
		if(!"0".equals(type)){
			treeList =new TemplateTreeTable(list).buildTree();
			if(treeList.size()==0){
				treeList=list;
			}
		}
		model.addAttribute("treeList", treeList);
		model.addAttribute("applyId",id);
		if("0".equals(type)){
			model.addAttribute("list", list);
			return "modules/template/treatTemplate/treatProjectItemBottom";
		} else if("1".equals(type)){
			return "modules/template/orderTemplate/orderProjectItemBottom";
		} else if("2".equals(type)){
			return "modules/template/prescTemplate/prescProjectItemBottom";
		} else {
			treatRecordItem.setApplyId(id);
			List<TreatRecordItem> listRecord = treatRecordItemService.findList(treatRecordItem);
			model.addAttribute("list", listRecord);
			return "modules/template/recordTemplate/recordProjectItemBottom";
		}
	}

	@RequiresPermissions("user")
	@RequestMapping(value = {"indexTreatChinese", ""})
	public String indexTreatChinese(Model model,DoctorTempla doctorTempla){
		TreatProjectItem treatProjectItem = new TreatProjectItem();
		treatProjectItem.setApplyId(doctorTempla.getId());
		List<TreatProjectItem> list = treatProjectItemService.findList(treatProjectItem);
		List<SysOrgDict> administrationDicts = SysOrgDictUtils.getSysDictList("ADMINISTRATION_DICT");
		List<SysOrgDict> clinicDict = SysOrgDictUtils.getSysDictList("CLINIC_DICT");
		model.addAttribute("administrationDicts", administrationDicts);
		List<PerformFreqDict> frequency = PerformFreqUtils.findFreqList();
		model.addAttribute("frequency", frequency);
		model.addAttribute("chinaList", list);
		return "modules/template/prescTemplate/prescChineseItem";
	}



	@RequiresPermissions("user")
	@RequestMapping(value = "formTreat")
	public String form(TreatProjectItem treatProjectItem,TreatRecordItem treatRecordItem,String type,String applyId, Model model) {
		treatProjectItem.setApplyId(applyId);
		model.addAttribute("treatProjectItem", treatProjectItem);
		DoctorTempla doctorTempla = doctorTemplaService.get(new DoctorTempla(applyId));
		model.addAttribute("doctorTempla",doctorTempla);
		if("0".equals(type)){
			return "modules/template/treatTemplate/treatProjectItemForm";
		} else if("1".equals(type)){
			return "modules/template/orderTemplate/orderProjectItemForm";
		} else if("2".equals(type)){
			return "modules/template/prescTemplate/prescProjectItemForm";
		} else {
//			treatRecordItem = treatRecordItemService.get(treatRecordItem.getId());
			model.addAttribute("treatRecordItem", treatRecordItem);
			return "modules/template/recordTemplate/recordProjectItemForm";
		}
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(TreatProjectItem treatProjectItem, Model model) {
	    StringData data=new StringData();
		int i=treatProjectItemService.save(treatProjectItem);
        if(i>0){
            data.setCode("success");
            data.setData("保存成功");
        }else{
            data.setCode("error");
            data.setData("保存失败");
        }
        return data;
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "saveChina")
	@ResponseBody
	public StringData saveChina(TreatProjectItem treatProjectItem, Model model) {
	    StringData data=new StringData();
		int i=treatProjectItemService.saveChina(treatProjectItem);
        if(i>0){
            data.setCode("success");
            data.setData("保存成功");
        }else{
            data.setCode("error");
            data.setData("保存失败");
        }
        return data;
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "saveRecord")
	@ResponseBody
	public StringData saveRecord(TreatRecordItem treatRecordItem, Model model) {
		StringData data=new StringData();
		int i = treatRecordItemService.save(treatRecordItem);
		if(i>0){
			data.setCode("success");
			data.setData("保存成功");
		}else{
			data.setCode("error");
			data.setData("保存失败");
		}
		return data;
	}



	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(TreatProjectItem treatProjectItem) {
	    StringData data=new StringData();
		int i=treatProjectItemService.delete(treatProjectItem);
        if(i>0){
            data.setCode("success");
            data.setData("删除成功");
        }else{
            data.setCode("error");
            data.setData("删除失败");
        }
		return data;
	}

}